package com.xiaobingby.easyuisys.controller;

import com.xiaobingby.common.controller.BaseController;
import com.xiaobingby.common.entity.EUDataGridQuery;
import com.xiaobingby.common.entity.EUDataGridResult;
import com.xiaobingby.common.result.JsonResult;
import com.xiaobingby.easyuisys.service.IRoleService;
import com.xiaobingby.easyuisys.service.IUserService;
import com.xiaobingby.sys.entity.Role;
import com.xiaobingby.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李磊
 * @date 2019/5/5
 */
@Controller("easyuiUserController")
@RequestMapping("/easyui")
public class UserController extends BaseController {
    @Autowired
    IUserService userService;
    @Autowired
    IRoleService roleService;

    @PutMapping("/userModifyPwd")
    @ResponseBody
    public JsonResult userModifyPwd(String oldPwd, String pwd){
        boolean success = userService.userModifyPwd(getCurrentLoginId(), oldPwd, pwd);
        if(success){
            return renderSuccess("修改密码成功");
        }else{
            return renderError("原始密码错误");
        }
    }

    @GetMapping("/userModifyPwd")
    public String userModifyPwd(){
        return "/easyui/userModifyPwd";
    }

    @GetMapping("/user/list")
    public String list(){
        return "/easyui/user/userManager";
    }

    @PostMapping("/user/list")
    @ResponseBody
    public EUDataGridResult list(EUDataGridQuery query, User user){
        return userService.listUser(query,user);
    }

    @RequiresRoles("2")
    @GetMapping("/user/add")
    public String addUser(){
        return "/easyui/user/userAdd";
    }

    @GetMapping("/user/modify")
    public String modifyUser(User user, Model model){
        model.addAttribute("user",user);
        return "/easyui/user/userModify";
    }

    @GetMapping("/user/roleSet")
    public String roleSet(Model model,Long id){
        List<Role> roles = roleService.selectList(null);
        model.addAttribute("roles",roles);
        model.addAttribute("id",id);
        return "/easyui/user/roleSet";
    }
}
